﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.HVAC;
using YArchitech.Revit;
using Transfer.Utils;

namespace YArchitech.Plumbing
{
	public class DrawMultiPipeOperation
	{
		public UIApplication UIApp { get; set; }

		public UIDocument UIDoc { get; set; }

		public DrawMultiPipeOperation(UIApplication uiApp, FormMultiPipeDraw form)
		{
			this.UIApp = uiApp;
			this.UIDoc = uiApp.ActiveUIDocument;
			this.DrawForm = form;
		}

		public bool Execute()
		{
			bool result = false;
			Transaction transaction = null;
			DrawingType operationType = this.DrawForm.OperationType;
			for (;;)
			{
				RevitVersionFuncs.ClearSelection(this.UIDoc.Selection);
				transaction = new Transaction(this.UIDoc.Document, "多管绘制");
				transaction.Start();
				XYZ xyz = null;
				try
				{
					DrawingType operationType2 = this.DrawForm.OperationType;
					this.DrawForm.cancelOperationType = CancelMessageType.CancelByUser;
					switch (this.DrawForm.OperationType)
					{
					case DrawingType.DrawNormally:
						this.DrawMultiPipeNormally(ref xyz);
						break;
					case DrawingType.DrawFollowingWall:
						this.DrawMultiPipeFollowingWall();
						break;
					case DrawingType.DrawFromExistingPipe:
						this.DrawForm.dataGridViewPipe.Enabled = false;
						this.DrawPipeFormExistingPipe();
						break;
					}
					transaction.Commit();
					result = true;
					continue;
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
				{
					ex.ToString();
					transaction.Commit();
					if (!this.DrawForm.dataGridViewPipe.Enabled)
					{
						this.DrawForm.dataGridViewPipe.Enabled = true;
					}
					if (this.DrawForm.cancelOperationType != CancelMessageType.EndCancel)
					{
						if (this.DrawForm.cancelOperationType == CancelMessageType.CancelByProgramme)
						{
							continue;
						}
						if (this.DrawForm.OperationType != DrawingType.DrawNormally || xyz != null)
						{
							this.DrawForm.OperationType = DrawingType.DrawNormally;
							continue;
						}
					}
				}
				catch (Exception ex2)
				{
					ex2.ToString();
					continue;
				}
				finally
				{
					if (transaction != null && ((int)transaction.GetStatus() == 1 || (int)transaction.GetStatus() == 5))
					{
						transaction.RollBack();
					}
				}
				break;
			}
			return result;
		}

		public void DrawMultiPipeNormally(ref XYZ startPt)
		{
			try
			{
				startPt = this.SelectStartPoint();
				if (startPt != null)
				{
					XYZ xyz = startPt;
					RevitVersionFuncs.ClearSelection(this.UIDoc.Selection);
					List<DrawPipeStruct> drawPipes = this.DrawForm.GetDrawPipes();
					if (drawPipes.Count != 0)
					{
						List<double> list = new List<double>();
						foreach (DrawPipeStruct drawPipeStruct in drawPipes)
						{
							list.Add(drawPipeStruct.offSet);
						}
						using (DrawMultiPipeJig drawMultiPipeJig = new DrawMultiPipeJig(this.UIDoc))
						{
							drawMultiPipeJig.DrawSize = true;
							drawMultiPipeJig.PointsList.Add(startPt);
							drawMultiPipeJig.PipeIntervals = list;
							List<Pipe> list2 = new List<Pipe>();
							List<Pipe> list3 = new List<Pipe>();
							try
							{
								for (;;)
								{
									drawMultiPipeJig.SetPromptState("", false, "", true);
									XYZ xyz2 = drawMultiPipeJig.PickEndPoint(startPt, "请选择绘制管道的终点：");
									startPt = xyz2;
									if (xyz2 == null)
									{
										goto IL_1D7;
									}
									if (xyz2.IsAlmostEqualTo(xyz))
									{
										break;
									}
									XYZ axisPoint = DrawMultiPipeUtility.GetAxisPoint(this.UIDoc, xyz2, xyz);
									if (axisPoint != null)
									{
										xyz2 = axisPoint;
									}
									drawMultiPipeJig.PointsList.Add(xyz2);
									xyz = xyz2;
									List<List<LineInfo>> list4 = DrawMultiPipeUtility.DrawMultiLines(drawMultiPipeJig.PointsList, null, null, list, false);
									if (list4.Count > 0)
									{
										XYZ item = drawMultiPipeJig.PointsList[0];
										List<LineInfo> pipeLineList = list4[0];
										list3 = new List<Pipe>();
										this.LayOneColumnPipes(pipeLineList, drawPipes, list3, list2);
										if (list2.Count > 0)
										{
											this.ConnectTwoColumnsPipes(list2, list3);
										}
										list2 = new List<Pipe>();
										foreach (Pipe item2 in list3)
										{
											list2.Add(item2);
										}
										drawMultiPipeJig.PointsList.Remove(item);
									}
								}
								MessageBox.Show("选择的两点过于接近，无法创建！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
								IL_1D7:;
							}
							catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
							{
								ex.ToString();
								throw ex;
							}
							catch (Exception ex2)
							{
								ex2.ToString();
								throw ex2;
							}
						}
					}
				}
			}
			catch (Exception ex3)
			{
				ex3.ToString();
				throw ex3;
			}
		}

		protected void DrawMultiPipeFollowingWall()
		{
			List<DrawPipeStruct> drawPipes = this.DrawForm.GetDrawPipes();
			if (drawPipes.Count == 0)
			{
				return;
			}
			try
			{
				List<Pipe> list = new List<Pipe>();
				List<Pipe> list2 = new List<Pipe>();
				List<List<Pipe>> list3 = new List<List<Pipe>>();
				new Dictionary<ElementId, List<Pipe>>();
				new List<Wall>();
				List<double> list4 = new List<double>();
				if (this.DrawForm.awdt == AlongWallDisType.PipeCentre)
				{
					for (int i = 0; i < drawPipes.Count; i++)
					{
						if (i == 0)
						{
							list4.Add(this.DrawForm.disToWall);
						}
						else
						{
							list4.Add(drawPipes.ElementAt(i).offSet);
						}
					}
				}
				else
				{
					for (int j = 0; j < drawPipes.Count; j++)
					{
						if (j == 0)
						{
							list4.Add(this.DrawForm.disToWall + drawPipes[0].pipeDiameter / 2.0);
						}
						else
						{
							list4.Add(drawPipes.ElementAt(j).offSet + drawPipes[j - 1].pipeDiameter / 2.0 + drawPipes[j].pipeDiameter / 2.0);
						}
					}
				}
				XYZ lastVer = null;
				Wall wall = null;
				IntersectionResultArray intersectionResultArray = null;
				for (;;)
				{
					Line line = null;
					XYZ xyz = new XYZ();
					Wall wall2 = this.SelectWall(out line, out xyz);
					if (line != null)
					{
						List<XYZ> list5 = new List<XYZ>();
						list5.Add(line.GetEndPoint(0));
						list5.Add(line.GetEndPoint(1));
						bool flag = false;
						bool flag2 = false;
						bool flag3 = false;
						new XYZ();
						if (wall != null)
						{
							Line line2 = (wall.Location as LocationCurve).Curve as Line;
							Line line3 = (wall2.Location as LocationCurve).Curve as Line;
							line2.Intersect(line3, out intersectionResultArray);
							if (intersectionResultArray != null)
							{
								XYZ xyzpoint = intersectionResultArray.get_Item(0).XYZPoint;
								if (list3.Count > 0)
								{
									if (list3.ElementAt(0).Count > 1)
									{
										using (List<List<Pipe>>.Enumerator enumerator = list3.GetEnumerator())
										{
											while (enumerator.MoveNext())
											{
												List<Pipe> source = enumerator.Current;
												Pipe pipe = source.ElementAt(0);
												Line line4 = (pipe.Location as LocationCurve).Curve as Line;
												XYZ xyzpoint2 = Line.CreateUnbound(line4.GetEndPoint(0), line4.Direction).Project(xyzpoint).XYZPoint;
												if (new YJKLine((pipe.Location as LocationCurve).Curve as Line).IsPointOnLine(xyzpoint2))
												{
													list.Add(source.ElementAt(0));
												}
												else
												{
													list.Add(source.ElementAt(1));
												}
											}
											goto IL_2DB;
										}
									}
									list.Add(list3.ElementAt(0).ElementAt(0));
									list.Add(list3.ElementAt(1).ElementAt(0));
									list.Add(list3.ElementAt(2).ElementAt(0));
								}
								IL_2DB:
								XYZ lastDir = (xyzpoint - (line2.GetEndPoint(0) + line2.GetEndPoint(1)) / 2.0).Normalize();
								XYZ newDir = ((line3.GetEndPoint(0) + line3.GetEndPoint(1)) / 2.0 - xyzpoint).Normalize();
								XYZ xyz2 = (xyzpoint.DistanceTo(line2.GetEndPoint(0)) > xyzpoint.DistanceTo(line2.GetEndPoint(1))) ? line2.GetEndPoint(0) : line2.GetEndPoint(1);
								XYZ xyz3 = (xyzpoint.DistanceTo(line3.GetEndPoint(0)) > xyzpoint.DistanceTo(line3.GetEndPoint(1))) ? line3.GetEndPoint(0) : line3.GetEndPoint(1);
								if (xyzpoint.DistanceTo(xyz2) < line2.Length - 1.0 || xyzpoint.DistanceTo(xyz3) < line3.Length - 1.0)
								{
									if (xyzpoint.DistanceTo(xyz2) < line2.Length - 1.0 && xyzpoint.DistanceTo(xyz3) < line3.Length - 1.0)
									{
										flag3 = true;
									}
									else
									{
										flag2 = true;
									}
								}
								flag = DrawMultiPipeUtility.ReverseSort(lastDir, lastVer, newDir, xyz, 0.01);
							}
							else
							{
								list.Clear();
							}
							if (flag)
							{
								list4.Reverse();
								drawPipes.Reverse();
							}
						}
						lastVer = xyz;
						wall = wall2;
						List<LineInfo> pipeLineList = DrawMultiPipeUtility.DrawMultiLines(list5, list4, xyz)[0];
						list2 = new List<Pipe>();
						this.LayOneColumnPipes(pipeLineList, drawPipes, list2, list);
						this.UIDoc.Document.Regenerate();
						list3.Clear();
						if (list.Count > 0)
						{
							if (flag)
							{
								list2.Reverse();
							}
							HYPipeConnect hypipeConnect = new HYPipeConnect();
							if (flag2)
							{
								if (wall != null)
								{
									Line line5 = (wall.Location as LocationCurve).Curve as Line;
									if (intersectionResultArray != null)
									{
										XYZ xyzpoint3 = intersectionResultArray.get_Item(0).XYZPoint;
										bool flag4 = xyzpoint3.DistanceTo(line5.GetEndPoint(0)) < 1.0 || xyzpoint3.DistanceTo(line5.GetEndPoint(1)) < 1.0;
										for (int k = 0; k < list2.Count; k++)
										{
											IList<Pipe> list6 = new List<Pipe>();
											if (flag4)
											{
												List<Pipe> source2 = new YJKPiping<Pipe>(list.ElementAt(k)).Separate((list.ElementAt(k).Location as LocationCurve).Curve.Project(xyzpoint3).XYZPoint);
												list6.Add(source2.ElementAt(0));
												list6.Add(source2.ElementAt(1));
												list6.Add(list2.ElementAt(k));
												list3.Add(new List<Pipe>
												{
													list2.ElementAt(k)
												});
											}
											else
											{
												List<Pipe> list7 = new YJKPiping<Pipe>(list2.ElementAt(k)).Separate((list2.ElementAt(k).Location as LocationCurve).Curve.Project(xyzpoint3).XYZPoint);
												list6.Add(list7.ElementAt(0));
												list6.Add(list7.ElementAt(1));
												list6.Add(list.ElementAt(k));
												list3.Add(list7);
											}
											string text = "";
											hypipeConnect.TeeFittingConnPipe(ref list6, "Y型三通", ref text);
										}
									}
								}
							}
							else if (flag3)
							{
								for (int l = 0; l < list2.Count; l++)
								{
									Curve curve = (list2.ElementAt(l).Location as LocationCurve).Curve as Line;
									Line line6 = (list.ElementAt(l).Location as LocationCurve).Curve as Line;
									curve.Intersect(line6, out intersectionResultArray);
									if (intersectionResultArray != null)
									{
										XYZ xyzpoint4 = intersectionResultArray.get_Item(0).XYZPoint;
										IList<Pipe> list8 = new List<Pipe>();
										List<Pipe> list9 = new YJKPiping<Pipe>(list2.ElementAt(l)).Separate(xyzpoint4);
										list8.Add(list9.ElementAt(0));
										list8.Add(list9.ElementAt(1));
										list3.Add(list9);
										List<Pipe> source3 = new YJKPiping<Pipe>(list.ElementAt(l)).Separate(xyzpoint4);
										list8.Add(source3.ElementAt(0));
										list8.Add(source3.ElementAt(1));
										hypipeConnect.NewCrossFitting(ref list8, true);
									}
								}
							}
							else if (Math.Abs(((list2.ElementAt(0).Location as LocationCurve).Curve as Line).Direction.AngleTo(((list.ElementAt(0).Location as LocationCurve).Curve as Line).Direction) - Math.PI*.5) < 0.01)
							{
								for (int m = 0; m < list2.Count; m++)
								{
									IList<Pipe> list10 = new List<Pipe>();
									list10.Add(list.ElementAt(m));
									list10.Add(list2.ElementAt(m));
									IList<XYZ> list11 = new List<XYZ>();
									list11.Add(((list.ElementAt(m).Location as LocationCurve).Curve.GetEndPoint(0) + (list.ElementAt(m).Location as LocationCurve).Curve.GetEndPoint(1)) / 2.0);
									list11.Add(((list2.ElementAt(m).Location as LocationCurve).Curve.GetEndPoint(0) + (list2.ElementAt(m).Location as LocationCurve).Curve.GetEndPoint(1)) / 2.0);
									string text2 = "";
									hypipeConnect.NewElbowFitting(list10, list11, "水管弯头", ref text2);
								}
							}
							else
							{
								this.ConnectTwoColumnsPipes(list, list2);
							}
						}
						list = new List<Pipe>();
						if (flag)
						{
							list2.Reverse();
							list3.Reverse();
						}
						if (!flag2 && !flag3)
						{
							foreach (Pipe item in list2)
							{
								list.Add(item);
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw ex;
			}
		}

		protected void DrawMultiPipeFormExistingPipe()
		{
			try
			{
				for (;;)
				{
					try
					{
						List<Pipe> list = this.SelectExistingPipe();
						if (list.Count > 0)
						{
							if (this.PipesIsParallel(list))
							{
								Pipe pipe = null;
								XYZ xyz = this.SelectPointOnPipe(ref pipe);
								if (xyz == null)
								{
									continue;
								}
								XYZ xyz2 = xyz;
								bool flag = false;
								using (List<Pipe>.Enumerator enumerator = list.GetEnumerator())
								{
									while (enumerator.MoveNext())
									{
										if (enumerator.Current.Id == pipe.Id)
										{
											flag = true;
											break;
										}
									}
								}
								if (!flag)
								{
									continue;
								}
								PipeFromInfo pipeFromInfo = new PipeFromInfo();
								pipeFromInfo.PipesFrom = list;
								pipeFromInfo.PointFrom = xyz;
								pipeFromInfo.LocatedPipe = pipe;
								using (DrawMultiPipeFromExistJig drawMultiPipeFromExistJig = new DrawMultiPipeFromExistJig(this.UIDoc))
								{
									drawMultiPipeFromExistJig.InfoOfPipeFrom = pipeFromInfo;
									drawMultiPipeFromExistJig.SettingDistance = this.DrawForm.pipeOffDis;
									List<Pipe> list2 = new List<Pipe>();
									List<Pipe> list3 = new List<Pipe>();
									List<double> list4 = new List<double>();
									List<DrawPipeStruct> list5 = null;
									bool flag2 = true;
									try
									{
										for (;;)
										{
											XYZ xyz3 = this.UIDoc.Selection.PickPoint("请选择绘制管道的终点：");
											if (xyz3.IsAlmostEqualTo(xyz2))
											{
												break;
											}
											XYZ axisPoint = DrawMultiPipeUtility.GetAxisPoint(this.UIDoc, xyz3, xyz2);
											if (axisPoint != null)
											{
												xyz3 = axisPoint;
											}
											drawMultiPipeFromExistJig.PointsList.Add(xyz3);
											xyz2 = xyz3;
											if (list5 == null)
											{
												list5 = DrawMultiPipeUtility.GetPipeInfo(this.UIDoc.Document, drawMultiPipeFromExistJig.InfoOfPipeFrom.SortedPipes, this.DrawForm.pipeOffDis);
												foreach (DrawPipeStruct drawPipeStruct in list5)
												{
													list4.Add(drawPipeStruct.offSet);
												}
											}
											List<List<LineInfo>> list6 = DrawMultiPipeUtility.DrawMultiLines(drawMultiPipeFromExistJig.PointsList, drawMultiPipeFromExistJig.StartFromPoint, null, list4, drawMultiPipeFromExistJig.IsAddFromPoint);
											if (drawMultiPipeFromExistJig.PointsList.Count > 1 && list6.Count > 1)
											{
												XYZ item = drawMultiPipeFromExistJig.PointsList[0];
												List<LineInfo> pipeLineList = list6[0];
												list3 = new List<Pipe>();
												this.LayOneColumnPipes(pipeLineList, list5, list3, list2);
												if (list2.Count != 0)
												{
													this.ConnectTwoColumnsPipes(list, list3);
												}
												if (flag2)
												{
													this.ConnectToTheExistingPipes(list3, drawMultiPipeFromExistJig.InfoOfPipeFrom.SortedPipes, xyz);
													flag2 = false;
												}
												list2 = new List<Pipe>();
												foreach (Pipe item2 in list3)
												{
													list2.Add(item2);
												}
												if (drawMultiPipeFromExistJig.PointsList.Count > 2)
												{
													drawMultiPipeFromExistJig.PointsList.Remove(item);
												}
												drawMultiPipeFromExistJig.IsAddFromPoint = false;
											}
										}
										MessageBox.Show("选择的两点过于接近，无法创建！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
									}
									catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
									{
										ex.ToString();
										List<List<LineInfo>> list7 = DrawMultiPipeUtility.DrawMultiLines(drawMultiPipeFromExistJig.PointsList, drawMultiPipeFromExistJig.StartFromPoint, null, list4, drawMultiPipeFromExistJig.IsAddFromPoint);
										if (list7.Count > 0)
										{
											XYZ xyz4 = drawMultiPipeFromExistJig.PointsList[0];
											List<LineInfo> pipeLineList2 = list7[0];
											list3 = new List<Pipe>();
											this.LayOneColumnPipes(pipeLineList2, list5, list3, list2);
											this.ConnectTwoColumnsPipes(list2, list3);
										}
										throw ex;
									}
									catch (Exception ex2)
									{
										ex2.ToString();
										throw ex2;
									}
									continue;
								}
							}
							YJKMessageBox.Information("所选管道不平行！");
						}
					}
					catch (Exception ex3)
					{
						ex3.ToString();
						throw ex3;
					}
				}
			}
			catch (Exception ex4)
			{
				ex4.ToString();
				throw ex4;
			}
		}

		protected List<Pipe> SelectExistingPipe()
		{
			List<Pipe> list = new List<Pipe>();
			if (this.UIDoc != null && this.UIDoc.Selection != null)
			{
				Selection selection = this.UIDoc.Selection;
				RevitVersionFuncs.ClearSelection(selection);
				IList<Element> list2 = selection.PickElementsByRectangle(new PipeFilter(), "请框选要引出的管线...");
				if (list2.Count > 1)
				{
					foreach (Element element in list2)
					{
						Pipe item = element as Pipe;
						list.Add(item);
					}
				}
			}
			return list;
		}

		protected XYZ SelectPointOnPipe(ref Pipe selectedPipe)
		{
			XYZ result = null;
			if (this.UIDoc != null && this.UIDoc.Selection != null)
			{
				Selection selection = this.UIDoc.Selection;
				RevitVersionFuncs.ClearSelection(selection);
				Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new PipeFilter(), "请点选管道上要引出管线的位置...");
				selectedPipe = (this.UIDoc.Document.GetElement(reference) as Pipe);
				result = reference.GlobalPoint;
			}
			return result;
		}

		protected bool PipesIsParallel(List<Pipe> pipes)
		{
			bool flag = false;
			if (pipes.Count > 0)
			{
				flag = true;
				IEnumerator enumerator = pipes.GetEnumerator();
				bool flag2 = enumerator.MoveNext();
				YJKLine YJKLine = new YJKLine(((enumerator.Current as Pipe).Location as LocationCurve).Curve as Line);
				flag2 = enumerator.MoveNext();
				while (flag2)
				{
					Line otherLine = ((enumerator.Current as Pipe).Location as LocationCurve).Curve as Line;
					flag &= YJKLine.IsParallel(otherLine);
					flag2 = enumerator.MoveNext();
				}
			}
			return flag;
		}

		protected List<Wall> GetConnectedWalls(List<Wall> wallList, Wall wall)
		{
			List<Wall> list = new List<Wall>();
			if (wall != null && wallList != null && wallList.Count > 0)
			{
				YJKLine YJKLine = new YJKLine((wall.Location as LocationCurve).Curve as Line);
				foreach (Wall wall2 in wallList)
				{
					if (wall2.Id != wall.Id)
					{
						Line otherLine = (wall2.Location as LocationCurve).Curve as Line;
						if (YJKLine.IntersectionPoint(otherLine) != null)
						{
							list.Add(wall2);
						}
					}
				}
			}
			return list;
		}

		private XYZ getxyz(Pipe pipe)
		{
			Line curve = (pipe.Location as LocationCurve).Curve as Line;
			return (curve.GetEndPoint(0) + curve.GetEndPoint(1)) / 2.0;
		}

		protected Wall SelectWall(out Line datumLine, out XYZ deviationDirection)
		{
			Wall wall = null;
			XYZ selectedPoint = new XYZ();
			if (this.UIDoc != null && this.UIDoc.Selection != null)
			{
				RevitVersionFuncs.ClearSelection(this.UIDoc.Selection);
				SelectedElement selectedElement;
				do
				{
					selectedElement = this.UIDoc.PickElementsByPoint("请选择墙面...", 0.05);
				}
				while (!(selectedElement.Element is Wall));
				wall = (selectedElement.Element as Wall);
				selectedPoint = selectedElement.SelectedPoint;
			}
			datumLine = this.GetFollowingWallDatumLine(wall, selectedPoint, out deviationDirection);
			return wall;
		}

		protected bool LayOneColumnPipes(List<LineInfo> pipeLineList, List<DrawPipeStruct> pipeInfoList, List<Pipe> newColumnPipes, List<Pipe> lastColumnPipes)
		{
			bool result = false;
			for (int i = 0; i < pipeLineList.Count; i++)
			{
				XYZ startPoint = pipeLineList[i].StartPoint;
				XYZ endPoint = pipeLineList[i].EndPoint;
				XYZ newXYZDeriction = YJKRevitTools.getNewXYZDeriction(startPoint, endPoint);
				HYCreatePipeHelper hycreatePipeHelper = new HYCreatePipeHelper();
				if (newXYZDeriction != null)
				{
					PipeType pipeType = pipeInfoList[i].pipeType;
					PipingSystemType pipingSystem = pipeInfoList[i].pipingSystem;
					double levelOffset = pipeInfoList[i].levelOffSet + this.UIDoc.Document.ActiveView.GenLevel.ProjectElevation * 304.8;
					Pipe item = hycreatePipeHelper.DrawThwartwisePipe(this.UIDoc.Document, 0.0, levelOffset, startPoint, endPoint, pipeInfoList[i].pipeType, pipeInfoList[i].pipeDiameter, pipeInfoList[i].pipingSystem, 0.0, newXYZDeriction);
					newColumnPipes.Add(item);
				}
			}
			return result;
		}

		protected bool ConnectToTheExistingPipes(List<Pipe> newPipes, List<Pipe> existingPipes, XYZ selectedPoint)
		{
			bool result = false;
			HYPipeConnect hypipeConnect = new HYPipeConnect();
			ConnectPipeManage connectPipeManage = new ConnectPipeManage();
			string empty = string.Empty;
			string familySymbolName = connectPipeManage.GetFamilySymbolName(ConnectPipeStyle.Tee);
			if (newPipes != null && existingPipes != null && newPipes.Count == existingPipes.Count)
			{
				for (int i = 0; i < newPipes.Count; i++)
				{
					YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(existingPipes[i]);
					XYZ xyzpoint = ((existingPipes[i].Location as LocationCurve).Curve as Line).Project(selectedPoint).XYZPoint;
					List<Pipe> list = hypiping.Separate(xyzpoint);
					list.Add(newPipes[i]);
					hypipeConnect.TeeFittingConnPipe(list, familySymbolName, ref empty);
					result = true;
				}
			}
			return result;
		}

		protected int ConnectTwoColumnsPipes(List<Pipe> fistColumn, List<Pipe> secondColumn)
		{
			int result = 0;
			if (fistColumn != null && secondColumn != null && fistColumn.Count == secondColumn.Count)
			{
				try
				{
					for (int i = 0; i < fistColumn.Count; i++)
					{
						Line line = (fistColumn[i].Location as LocationCurve).Curve as Line;
						Line line2 = (secondColumn[i].Location as LocationCurve).Curve as Line;
						YJKLine YJKLine = new YJKLine(line);
						YJKLine YJKLine2 = new YJKLine(line2);
						if (!YJKLine.IsLineOnLine(line2) && !YJKLine.IsXYParallel(line2))
						{
							XYZ xyz = (YJKLine.EndPoint - YJKLine.StartPoint).Normalize();
							XYZ xyz2 = (YJKLine2.EndPoint - YJKLine2.StartPoint).Normalize();
							double val = xyz.AngleTo(xyz2) * 180.0 / Math.PI;
							YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
							Pipe pipe = fistColumn[i];
							Pipe pipe2 = secondColumn[i];
							if (Geometry.Lessthan_Or_Equal(val, 90.0))
							{
								hypipeConnectorHelper.pipeconnectpipe(pipe, this.getxyz(pipe), pipe2, this.getxyz(pipe2), this.UIDoc.Document);
							}
							else
							{
								hypipeConnectorHelper.connect45Elbow(pipe, this.getxyz(pipe), pipe2, this.getxyz(pipe2), this.UIDoc.Document);
							}
						}
					}
					result = 1;
				}
				catch (Exception ex)
				{
					ex.ToString();
					result = 2;
				}
			}
			return result;
		}

		protected bool PipeIsConnected(List<Pipe> pipes)
		{
			if (pipes != null && pipes.Count > 0)
			{
				bool flag = true;
				foreach (Pipe pipe in pipes)
				{
					foreach (object obj in pipe.ConnectorManager.Connectors)
					{
						Connector connector = (Connector)obj;
						flag &= connector.IsConnected;
					}
				}
				return flag;
			}
			return true;
		}

		protected XYZ SelectStartPoint()
		{
			XYZ result = null;
			if (this.UIDoc != null && this.UIDoc.Selection != null)
			{
				Selection selection = this.UIDoc.Selection;
				RevitVersionFuncs.ClearSelection(selection);
				result = selection.PickPoint("请选绘制管道的起点：");
			}
			return result;
		}

		private Element GetElementSameName(ICollection<Element> collection, string elemName)
		{
			Element result = null;
			foreach (Element element in collection)
			{
				if (element != null && element.Name == elemName)
				{
					result = element;
				}
			}
			return result;
		}

		private Options GetGeomOptions()
		{
			Options options = this.UIApp.Application.Create.NewGeometryOptions();
			options.ComputeReferences = true;
			options.DetailLevel = (ViewDetailLevel)3;
			return options;
		}

		private Line GetFollowingWallDatumLine(Wall wall, XYZ selectedPoint, out XYZ deviationDirection)
		{
			XYZ endPoint = (wall.Location as LocationCurve).Curve.GetEndPoint(0);
			XYZ endPoint2 = (wall.Location as LocationCurve).Curve.GetEndPoint(1);
			XYZ xyz = new XYZ(selectedPoint.X, selectedPoint.Y, 0.0);
			Line line = Line.CreateBound(new XYZ(endPoint.X, endPoint.Y, 0.0), new XYZ(endPoint2.X, endPoint2.Y, 0.0));
			XYZ xyz2 = (Line.CreateUnbound(line.GetEndPoint(0), line.Direction).Project(xyz).XYZPoint - xyz).Normalize();
			GeometryElement geometryElement = wall.get_Geometry(this.GetGeomOptions());
			Line line2 = null;
			foreach (GeometryObject geometryObject in geometryElement)
			{
				foreach (object obj in (geometryObject as Solid).Faces)
				{
					PlanarFace planarFace = ((Face)obj) as PlanarFace;
					UV uv = new UV(planarFace.Origin.X, planarFace.Origin.Y);
					XYZ xyz3 = planarFace.ComputeNormal(uv);
					if (xyz2.IsAlmostEqualTo(-xyz3))
					{
						line2 = Line.CreateUnbound(planarFace.Origin, line.Direction.Normalize());
						break;
					}
				}
			}
			if (line2 != null)
			{
				XYZ xyz4 = line2.Project(endPoint).XYZPoint;
				XYZ xyz5 = line2.Project(endPoint2).XYZPoint;
				deviationDirection = -xyz2;
				if (wall.Document.IsLinked)
				{
					LinkedDocument linkedDocument = this.UIDoc.Document.GetLinkedDocument(wall.Id);
					xyz4 = linkedDocument.GetPointInLinkDocument(xyz4);
					xyz5 = linkedDocument.GetPointInLinkDocument(xyz5);
				}
				return Line.CreateBound(xyz4, xyz5);
			}
			deviationDirection = new XYZ();
			return null;
		}

		private List<ExistingPipeStruct> OrderPipes(List<Pipe> selectedPipes, out XYZ selPoint)
		{
			List<ExistingPipeStruct> list = new List<ExistingPipeStruct>();
			Pipe pipe = null;
			XYZ xyz = this.SelectPointOnPipe(this.UIDoc, selectedPipes, ref pipe);
			selPoint = new XYZ(xyz.X, xyz.Y, 0.0);
			if (pipe == null)
			{
				return null;
			}
			XYZ xyz2 = new XYZ(xyz.X, xyz.Y, 0.0);
			XYZ endPoint = (pipe.Location as LocationCurve).Curve.GetEndPoint(0);
			XYZ endPoint2 = (pipe.Location as LocationCurve).Curve.GetEndPoint(1);
			XYZ xyz3 = (endPoint2 - endPoint).Normalize();
			if (selPoint.DistanceTo(endPoint) < selPoint.DistanceTo(endPoint2))
			{
				xyz3 = -xyz3;
			}
			foreach (Pipe pipe2 in selectedPipes)
			{
				ExistingPipeStruct existingPipeStruct = new ExistingPipeStruct();
				if (pipe2.Id == pipe.Id)
				{
					existingPipeStruct.pipe = pipe2;
					existingPipeStruct.verticalDirection = new XYZ();
					existingPipeStruct.distance = 0.0;
					existingPipeStruct.serialNumber = 0;
					existingPipeStruct.pipeDirection = xyz3;
				}
				else
				{
					existingPipeStruct.pipe = pipe2;
					XYZ endPoint3 = (pipe2.Location as LocationCurve).Curve.GetEndPoint(0);
					XYZ endPoint4 = (pipe2.Location as LocationCurve).Curve.GetEndPoint(1);
					Line line = Line.CreateBound(new XYZ(endPoint3.X, endPoint3.Y, 0.0), new XYZ(endPoint4.X, endPoint4.Y, 0.0));
					XYZ xyzpoint = Line.CreateUnbound(new XYZ(endPoint3.X, endPoint3.Y, 0.0), line.Direction).Project(xyz2).XYZPoint;
					existingPipeStruct.verticalDirection = -(xyz2 - xyzpoint).Normalize();
					existingPipeStruct.distance = xyzpoint.DistanceTo(xyz2);
					existingPipeStruct.serialNumber = 1;
				}
				list.Add(existingPipeStruct);
			}
			int num = 1;
			int num2 = 1;
			list = (from p in list.AsEnumerable<ExistingPipeStruct>()
			orderby p.distance
			select p).ToList<ExistingPipeStruct>();
			XYZ verticalDirection = list.First((ExistingPipeStruct p) => p.serialNumber == 1).verticalDirection;
			foreach (ExistingPipeStruct existingPipeStruct2 in list)
			{
				if (existingPipeStruct2.pipe.Id != pipe.Id)
				{
					if (existingPipeStruct2.verticalDirection.IsAlmostEqualTo(verticalDirection))
					{
						existingPipeStruct2.serialNumber = num;
						num++;
					}
					else
					{
						existingPipeStruct2.serialNumber = num2;
						num2++;
					}
				}
			}
			return list;
		}

		private List<ExistingPipeStruct> OrderPipes(List<Pipe> selectedPipes, XYZ selPoint)
		{
			List<ExistingPipeStruct> list = new List<ExistingPipeStruct>();
			Pipe selectedPipe = this.GetSelectedPipe(selectedPipes, selPoint);
			XYZ xyz = new XYZ(selPoint.X, selPoint.Y, 0.0);
			XYZ xyz2 = new XYZ(xyz.X, xyz.Y, 0.0);
			XYZ endPoint = (selectedPipe.Location as LocationCurve).Curve.GetEndPoint(0);
			XYZ pipeDirection = ((selectedPipe.Location as LocationCurve).Curve.GetEndPoint(1) - endPoint).Normalize();
			foreach (Pipe pipe in selectedPipes)
			{
				ExistingPipeStruct existingPipeStruct = new ExistingPipeStruct();
				if (pipe.Id == selectedPipe.Id)
				{
					existingPipeStruct.pipe = pipe;
					existingPipeStruct.verticalDirection = new XYZ();
					existingPipeStruct.distance = 0.0;
					existingPipeStruct.serialNumber = 0;
					existingPipeStruct.pipeDirection = pipeDirection;
				}
				else
				{
					existingPipeStruct.pipe = pipe;
					XYZ endPoint2 = (pipe.Location as LocationCurve).Curve.GetEndPoint(0);
					XYZ endPoint3 = (pipe.Location as LocationCurve).Curve.GetEndPoint(1);
					Line line = Line.CreateBound(new XYZ(endPoint2.X, endPoint2.Y, 0.0), new XYZ(endPoint3.X, endPoint3.Y, 0.0));
					XYZ xyzpoint = Line.CreateUnbound(new XYZ(endPoint2.X, endPoint2.Y, 0.0), line.Direction).Project(xyz2).XYZPoint;
					existingPipeStruct.verticalDirection = -(xyz2 - xyzpoint).Normalize();
					existingPipeStruct.distance = xyzpoint.DistanceTo(xyz2);
					existingPipeStruct.serialNumber = 1;
				}
				list.Add(existingPipeStruct);
			}
			int num = 1;
			int num2 = 1;
			list = (from p in list.AsEnumerable<ExistingPipeStruct>()
			orderby p.distance
			select p).ToList<ExistingPipeStruct>();
			XYZ verticalDirection = list.First((ExistingPipeStruct p) => p.serialNumber == 1).verticalDirection;
			foreach (ExistingPipeStruct existingPipeStruct2 in list)
			{
				if (existingPipeStruct2.pipe.Id != selectedPipe.Id)
				{
					if (existingPipeStruct2.verticalDirection.IsAlmostEqualTo(verticalDirection))
					{
						existingPipeStruct2.serialNumber = num;
						num++;
					}
					else
					{
						existingPipeStruct2.serialNumber = num2;
						num2++;
					}
				}
			}
			return (from p in list
			orderby p.serialNumber
			select p).ToList<ExistingPipeStruct>();
		}

		private XYZ SelectPointOnPipe(UIDocument UIDoc, List<Pipe> selectedPipes, ref Pipe selectedPipe)
		{
			List<Element> list = new List<Element>();
			foreach (Pipe item in selectedPipes)
			{
				list.Add(item);
			}
			XYZ result = null;
			if (UIDoc != null && UIDoc.Selection != null)
			{
				Reference reference = UIDoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new ElemInElementsFilter(list), "请点选管道上要引出管线的位置...");
				selectedPipe = (UIDoc.Document.GetElement(reference) as Pipe);
				result = reference.GlobalPoint;
			}
			return result;
		}

		private Pipe GetSelectedPipe(List<Pipe> selectedPipes, XYZ selPoint)
		{
			Dictionary<Pipe, double> dicPipeDis = new Dictionary<Pipe, double>();
			XYZ xyz = new XYZ(selPoint.X, selPoint.Y, 0.0);
			foreach (Pipe pipe in selectedPipes)
			{
				XYZ endPoint = (pipe.Location as LocationCurve).Curve.GetEndPoint(0);
				XYZ endPoint2 = (pipe.Location as LocationCurve).Curve.GetEndPoint(1);
				Line line = Line.CreateBound(new XYZ(endPoint.X, endPoint.Y, 0.0), new XYZ(endPoint2.X, endPoint2.Y, 0.0));
				Line line2 = Line.CreateUnbound(new XYZ(endPoint.X, endPoint.Y, 0.0), line.Direction);
				dicPipeDis.Add(pipe, line2.Project(xyz).Distance);
			}
			return (from x in dicPipeDis.Keys
			select new
			{
				x = x,
				y = dicPipeDis[x]
			} into x
			orderby x.y
			select x).First().x;
		}

		private void DrawPipeFormExistingPipe()
		{
			try
			{
				bool flag = false;
				for (;;)
				{
					try
					{
						List<Pipe> list = this.SelectExistingPipe();
						if (list.Count > 0)
						{
							if (this.PipesIsParallel(list))
							{
								foreach (Pipe pipe in list)
								{
									Parameter parameter = pipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM);
									if (this.DrawForm.pipeLevel != 0.0 && parameter.AsDouble() * 3.0 * 304.8 > Math.Abs(this.DrawForm.pipeLevel))
									{
										YJKMessageBox.Show("与原有管道高差设置过小");
										FormPipeDeriDraw formPipeDeriDraw = new FormPipeDeriDraw();
										formPipeDeriDraw.ShowDialog();
										this.DrawForm.pipeLevel = formPipeDeriDraw.PipeLevel;
										return;
									}
									if (YJKRevitTools.BeSlopePipe(pipe))
									{
										YJKMessageBox.Show("不支持对带坡度管道进行引出绘制！");
										return;
									}
								}
								XYZ xyz;
								List<ExistingPipeStruct> list2 = this.OrderPipes(list, out xyz);
								if (xyz == null)
								{
									continue;
								}
								XYZ xyz2 = xyz;
								List<Pipe> pipes = new List<Pipe>();
								Dictionary<Pipe, Pipe> dictionary = new Dictionary<Pipe, Pipe>();
								new List<double>();
								List<DrawPipeStruct> list3 = null;
								bool flag2 = true;
								try
								{
									for (;;)
									{
										XYZ xyz3 = null;
										using (DrawPipeFromExistJig drawPipeFromExistJig = new DrawPipeFromExistJig(this.UIDoc))
										{
											drawPipeFromExistJig.StartPoint = xyz2;
											drawPipeFromExistJig.ExistPipeStructs = list2;
											drawPipeFromExistJig.SettingDistance = this.DrawForm.pipeOffDis;
											drawPipeFromExistJig.DeriPipeOffSet = this.DrawForm.dpos;
											drawPipeFromExistJig.DrawSize = true;
											Application.DoEvents();
											drawPipeFromExistJig.SetPromptState("", false, "", true);
											xyz3 = drawPipeFromExistJig.PickEndPoint(xyz2, "请选择绘制管道的终点：");
											drawPipeFromExistJig.Dispose();
										}
										if (xyz3 == null)
										{
											break;
										}
										if (xyz3.IsAlmostEqualTo(xyz2))
										{
											goto IL_290;
										}
										if (list3 == null)
										{
											list3 = DrawMultiPipeUtility.GetPipeInfo(this.UIDoc.Document, list2, this.DrawForm.pipeOffDis, this.DrawForm.pipeLevel);
										}
										List<List<LineInfo>> list4 = DrawMultiPipeUtility.DrawMutilFromExistingPipes(list2, xyz2, xyz3, this.DrawForm.dpos, this.DrawForm.pipeOffDis);
										dictionary = new Dictionary<Pipe, Pipe>();
										this.LayOneColumnPipes(list4[0], list3, dictionary);
										if (flag2)
										{
											if (this.DrawForm.pipeLevel == 0.0)
											{
												this.ConnectToTheExistingPipes(dictionary, xyz);
											}
											else
											{
												this.ConnectToThePipesLevel(dictionary, this.DrawForm.pipeLevel);
											}
											flag2 = false;
										}
										else
										{
											this.ConnectTwoColumnsPipes(pipes, dictionary);
										}
										list2 = this.OrderPipes(this.GetPipeList(dictionary), xyz2);
										pipes = this.GetLastColumnPipes(list2);
										xyz2 = xyz3;
									}
									flag = true;
									goto IL_2A3;
									IL_290:
									MessageBox.Show("选择的两点过于接近，无法创建！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
									IL_2A3:
									if (flag)
									{
										break;
									}
									continue;
								}
								catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
								{
									ex.ToString();
									throw ex;
								}
								catch (Exception ex2)
								{
									ex2.ToString();
									throw ex2;
								}
							}
							YJKMessageBox.Information("所选管道不平行！");
						}
						continue;
					}
					catch (Exception ex3)
					{
						ex3.ToString();
						throw;
					}
					break;
				}
			}
			catch (Exception ex4)
			{
				ex4.ToString();
				throw ex4;
			}
		}

		private List<Pipe> GetLastColumnPipes(List<ExistingPipeStruct> pipes)
		{
			List<Pipe> list = new List<Pipe>();
			foreach (ExistingPipeStruct existingPipeStruct in pipes)
			{
				list.Add(existingPipeStruct.pipe);
			}
			return list;
		}

		protected bool LayOneColumnPipes(List<LineInfo> pipeLineList, List<DrawPipeStruct> pipeInfoList, Dictionary<Pipe, Pipe> newColumnPipes)
		{
			bool result = false;
			for (int i = 0; i < pipeLineList.Count; i++)
			{
				XYZ startPoint = pipeLineList[i].StartPoint;
				XYZ endPoint = pipeLineList[i].EndPoint;
				XYZ newXYZDeriction = YJKRevitTools.getNewXYZDeriction(startPoint, endPoint);
				HYCreatePipeHelper hycreatePipeHelper = new HYCreatePipeHelper();
				if (newXYZDeriction != null)
				{
					PipeType pipeType = pipeInfoList[i].pipeType;
					PipingSystemType pipingSystem = pipeInfoList[i].pipingSystem;
					double levelOffset = pipeInfoList[i].levelOffSet + this.UIDoc.Document.ActiveView.GenLevel.ProjectElevation * 304.8;
					Pipe value = hycreatePipeHelper.DrawThwartwisePipe(this.UIDoc.Document, 0.0, levelOffset, startPoint, endPoint, pipeInfoList[i].pipeType, pipeInfoList[i].pipeDiameter, pipeInfoList[i].pipingSystem, 0.0, newXYZDeriction);
					newColumnPipes.Add(pipeInfoList[i].Pipe, value);
				}
			}
			return result;
		}

		private void ConnectTwoColumnsPipes(List<Pipe> pipes, Dictionary<Pipe, Pipe> dicPipes)
		{
			try
			{
				int num = 0;
				foreach (KeyValuePair<Pipe, Pipe> keyValuePair in dicPipes)
				{
					Line line = (pipes[num].Location as LocationCurve).Curve as Line;
					Line line2 = (keyValuePair.Value.Location as LocationCurve).Curve as Line;
					YJKLine YJKLine = new YJKLine(line);
					YJKLine YJKLine2 = new YJKLine(line2);
					if (!YJKLine.IsLineOnLine(line2) && !YJKLine.IsXYParallel(line2))
					{
						XYZ xyz = (YJKLine.EndPoint - YJKLine.StartPoint).Normalize();
						XYZ xyz2 = (YJKLine2.EndPoint - YJKLine2.StartPoint).Normalize();
						double val = xyz.AngleTo(xyz2) * 180.0 / Math.PI;
						YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
						Pipe pipe = pipes[num];
						Pipe value = keyValuePair.Value;
						if (Geometry.Lessthan_Or_Equal(val, 90.0))
						{
							hypipeConnectorHelper.pipeconnectpipe(pipe, this.getxyz(pipe), value, this.getxyz(value), this.UIDoc.Document);
						}
						else
						{
							hypipeConnectorHelper.connect45Elbow(pipe, this.getxyz(pipe), value, this.getxyz(value), this.UIDoc.Document);
						}
					}
					num++;
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
		}

		private void ConnectToTheExistingPipes(Dictionary<Pipe, Pipe> dicPipes, XYZ selectedPoint)
		{
			HYPipeConnect hypipeConnect = new HYPipeConnect();
			ConnectPipeManage connectPipeManage = new ConnectPipeManage();
			string connMSG = string.Empty;
			string familySymbolName = connectPipeManage.GetFamilySymbolName(ConnectPipeStyle.Tee);
			foreach (KeyValuePair<Pipe, Pipe> item in dicPipes)
			{
				Line line = (item.Key.Location as LocationCurve).Curve as Line;
				Line line2 = (item.Value.Location as LocationCurve).Curve as Line;
				if (line != null && line2 != null)
				{
					Curve curve = Line.CreateUnbound(line.GetEndPoint(0), line.Direction);
					Line line3 = Line.CreateUnbound(line2.GetEndPoint(0), line2.Direction);
					IntersectionResultArray intersectionResultArray = null;
					curve.Intersect(line3, out intersectionResultArray);
					if (intersectionResultArray != null)
					{
						YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(item.Key);
						XYZ xyzpoint = intersectionResultArray.get_Item(0).XYZPoint;
						if (hypiping.IsPointOnPipingCurve(xyzpoint))
						{
							if (!this.GetConnectedType(item, xyzpoint))
							{
								connMSG = this.ConnectByElbow(connMSG, item);
							}
							else
							{
								IList<Pipe> list = hypiping.Separate(intersectionResultArray.get_Item(0).XYZPoint);
								list.Add(item.Value);
								hypipeConnect.TeeFittingConnPipe(list, familySymbolName, ref connMSG);
							}
						}
						else
						{
							connMSG = this.ConnectByElbow(connMSG, item);
						}
					}
				}
			}
		}

		private string ConnectByElbow(string connMSG, KeyValuePair<Pipe, Pipe> item)
		{
			if (Math.Abs(((item.Key.Location as LocationCurve).Curve as Line).Direction.AngleTo(((item.Value.Location as LocationCurve).Curve as Line).Direction) - Math.PI*.5) < 0.01)
			{
				new HYPipeConnect().NewElbowFitting(new List<Pipe>
				{
					item.Key,
					item.Value
				}, new List<XYZ>
				{
					((item.Key.Location as LocationCurve).Curve.GetEndPoint(0) + (item.Key.Location as LocationCurve).Curve.GetEndPoint(1)) / 2.0,
					((item.Value.Location as LocationCurve).Curve.GetEndPoint(0) + (item.Value.Location as LocationCurve).Curve.GetEndPoint(1)) / 2.0
				}, "水管弯头", ref connMSG);
			}
			else
			{
				this.ConnectTwoColumnsPipes(new List<Pipe>
				{
					item.Key
				}, new List<Pipe>
				{
					item.Value
				});
			}
			return connMSG;
		}

		private bool GetConnectedType(KeyValuePair<Pipe, Pipe> item, XYZ splitPoint)
		{
			bool result = true;
			XYZ endPoint = (item.Key.Location as LocationCurve).Curve.GetEndPoint(0);
			XYZ endPoint2 = (item.Key.Location as LocationCurve).Curve.GetEndPoint(1);
			if (((splitPoint.DistanceTo(endPoint) >= splitPoint.DistanceTo(endPoint2)) ? splitPoint.DistanceTo(endPoint2) : splitPoint.DistanceTo(endPoint)) < item.Key.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble() * 5.0)
			{
				result = false;
			}
			return result;
		}

		private List<Pipe> GetPipeList(Dictionary<Pipe, Pipe> dicPipes)
		{
			List<Pipe> list = new List<Pipe>();
			foreach (KeyValuePair<Pipe, Pipe> keyValuePair in dicPipes)
			{
				list.Add(keyValuePair.Value);
			}
			return list;
		}

		private void ConnectToThePipesLevel(Dictionary<Pipe, Pipe> dicPipes, double level)
		{
			HYPipeConnect hypipeConnect = new HYPipeConnect();
			ConnectPipeManage connectPipeManage = new ConnectPipeManage();
			string text = "";
			string familySymbolName = connectPipeManage.GetFamilySymbolName(ConnectPipeStyle.Tee);
			try
			{
				foreach (KeyValuePair<Pipe, Pipe> keyValuePair in dicPipes)
				{
					Line line = (keyValuePair.Key.Location as LocationCurve).Curve as Line;
					Line line2 = (keyValuePair.Value.Location as LocationCurve).Curve as Line;
					if (line != null && line2 != null)
					{
						XYZ hyendPoint = line.GetEndPoint(0);
						XYZ start = new XYZ(hyendPoint.X, hyendPoint.Y, hyendPoint.Z + level / 304.8);
						XYZ hyendPoint2 = line.GetEndPoint(1);
						XYZ end = new XYZ(hyendPoint2.X, hyendPoint2.Y, hyendPoint2.Z + level / 304.8);
						YJKLine YJKLine = new YJKLine(start, end);
						YJKLine.MakeUnBound();
						YJKLine YJKLine2 = new YJKLine(line2.GetEndPoint(0), line2.GetEndPoint(1));
						YJKLine2.MakeUnBound();
						XYZ xyz = YJKLine.IntersectionPoint(YJKLine2.Line);
						if (xyz != null)
						{
							YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(keyValuePair.Key);
							XYZ xyz2 = new XYZ(xyz.X, xyz.Y, xyz.Z - level / 304.8);
							Pipe pipe = YJKRevitTools.NewPipe(keyValuePair.Key, xyz, xyz2);
							Dictionary<Pipe, Pipe> dictionary = new Dictionary<Pipe, Pipe>();
							Dictionary<Pipe, Pipe> dictionary2 = new Dictionary<Pipe, Pipe>();
							dictionary.Add(keyValuePair.Key, pipe);
							dictionary2.Add(pipe, keyValuePair.Value);
							foreach (KeyValuePair<Pipe, Pipe> item in dictionary)
							{
								if (hypiping.IsPointOnPipingCurve(xyz2))
								{
									if (!this.GetConnectedType(item, xyz2))
									{
										this.ConnectByElbow(text, item);
									}
									else
									{
										IList<Pipe> list = hypiping.Separate(xyz2);
										list.Add(item.Value);
										hypipeConnect.TeeFittingConnPipe(list, familySymbolName, ref text);
									}
								}
								else
								{
									this.ConnectByElbow(text, item);
								}
							}
							foreach (KeyValuePair<Pipe, Pipe> item2 in dictionary2)
							{
								this.ConnectByElbow(text, item2);
							}
						}
					}
				}
			}
			catch (Exception)
			{
				YJKMessageBox.Show(text);
			}
		}

		public DrawingType DrawingOperationType = DrawingType.undifined;

		public FormMultiPipeDraw DrawForm;
	}
}
